$(function () {
    var markId = getUrlString('markid'),nickName;

    function getUserInfo () {
        getData('post','/niu/apiv3/member_userinfo.php',{'markid':markId},function (data) {
            console.log(data);
            let res = data.data;
            if (res.birthday === ''){
                $('#my-input').val('2020-01-01').calendar({
                    value: ['2020-01-01']
                });
            } else {
                $('#my-input').val(res.birthday).calendar({
                    value: [res.birthday]
                });
            }
            $('#phone').val(res.phone);
            nickName = res.nick;
            $('#nick').val(res.nick);
            $('#userintro').val(res.userintro);
            $('#faceurl').attr('src',res.faceurl).click(function () {
                getnative("dancool://uploadfile=1");
            });
            $('#sex').val(res.sex);
            $('#iscabhidden').val(res.iscabhidden);
            $('#islevelhidden').val(res.islevelhidden);
            $('#tuisong').val(res.setpushmode)

        });
    }
    getUserInfo();

    function isWJC (words){
        getData('post','/niu/apiv3/weibo_blackuser.php?method=checkwords',{'markid':markId,'words':words},function (data) {
            console.log(data);
            if (data.resultcode === '0'){
                modification()
            }else{
                $.toast('您的昵称包含违禁词')
            }

        })
    }

    function modification () {
        let nick = $('#nick').val();
        let phone = $('#phone').val();
        let pw = $('#code').val();
        let sex = $('#sex ').val();
        let birthday = $('#my-input').val();
        let faceurl = $('#faceurl').attr('src');
        let iscabhidden = $('#iscabhidden').val();
        let islevelhidden = $('#islevelhidden').val();
        let setpushmode = $('#tuisong').val();
        let userintro = $('#userintro').val();
        let is_nick = 1;
        if (nick === ''){
            $.toast('昵称不能为空');
            return
        }

        if (birthday === ''){
            $.toast('生日不能为空');
            return
        }

        if (sex === ''){
            $.toast('性别不能为空');
            return
        }

        if (nickName === nick){
            is_nick = 1
        } else {
            is_nick = 0;
        }
        console.log(is_nick);
        let data = {
            'markid':markId,
            'nick':nick,
            'phone':phone,
            'sex':sex,
            'birthday':birthday,
            'faceurl':faceurl,
            'set1':iscabhidden,
            'set2':userintro,
            'set4':islevelhidden,
            'setpushmode':setpushmode,
            'pw':pw,
            'is_nick':is_nick
        };
        console.log(data);
        getData('post', '/niu/api/member_dosetting.php', data, function (data) {
            console.log(data);
            if (data.resultcode === '0'){
                $.toast(data.msg);
                setTimeout(function () {
                    nativeCommand("dancool://activity=reflashfin")
                },500)
            } else {
                $.toast(data.msg)
            }
        })
    }

    $('#qrxg').click(function () {
        let nick = $('#nick').val();
        isWJC(nick);

    });

    //获取验证码
    $('#getCode').click(function () {
        getCode();
    });


    function getCode () {

        let phone = $('#phone').val();
        if (!phoneNumberVer(phone)){
            return
        }
        let data = {
            'phone':phone,
            'markid':markId,
        };
        getData('post', '/niu/api/member_dosetting_sms.php', data, function (data) {
            console.log(data);
            if (data.resultcode === '0'){
                $('#getCode').hide();
                $('#getCodeNum').show();
                var timer = null;
                var count = 60;
                timer = setInterval(function(){
                    count--;
                    $('#getCodeNum').html(count+'S');
                    if (count <=0) {
                        clearInterval(timer);
                        $('#getCodeNum').hide();
                        $('#getCode').show();
                    }
                },1000);
            } else {
                $.toast(data.msg)
            }
        })
    }
});

function getnative(actionurl) {
    var u = navigator.userAgent;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    if (isiOS) {
        window.webkit.messageHandlers.getnative.postMessage(actionurl);
    }else {
        ikags.getnative(actionurl)
    }
}

function jsnativeresult (aaa,bbb,ccc) {
    console.log(ccc);
    if (ccc !== '' && ccc !== null){
        $('#faceurl').attr('src',ccc)
    }

}
